package com.ctbri.vSearch.dao;

import com.alibaba.fastjson.JSONObject;
import com.ctbri.vSearch.bean.proto.LinkProto;
import com.ctbri.vSearch.bean.proto.NodeProto;
import com.ctbri.vSearch.frame.Neo4jPooledConnection;
import com.ctbri.vSearch.frame.RedisPooledConnection;
import com.ctbri.vSearch.util.CategoryStr2Int;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.Jedis;

/**
 * Created by xuexi on 2017/4/24.
 */
@Repository
public class Neo4j2Redis {

    public static Long cntNode = 1L;
    public static Long cntLink = 1L;
    public void createNeo4j2RedisByWord(String word, Jedis client){
        client.set(word+"_neo4j","0");
        client.expire(word,60*30); //1800s失效时间
    }
    public void putNode2Redis(String word, String name, String category, String label, double value, Jedis client, boolean isCenter)
            throws IOException {
        NodeProto.Node.Builder nodeBuilder =  NodeProto.Node.newBuilder();
        nodeBuilder.setName(name);
        nodeBuilder.setCategory(CategoryStr2Int.getCategory(category));
        nodeBuilder.setLabel(label);
        nodeBuilder.setValue(value);
        NodeProto.Node nodeB = nodeBuilder.build();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        nodeB.writeTo(output);
        byte[] byteArray = output.toByteArray();
        String s =new String(byteArray,"ISO-8859-1");
        if(isCenter)
            putCenterNode2Redis(word, s, client);
        else
            putOtherNode2Redis(word, s, client);

    }

    public void putCenterNode2Redis(String word,String s, Jedis client)
            throws IOException {
        HashMap<String, String> map = new HashMap<>();
        map.put("0",s);
        client.hmset(word + "_node_neo4j", map);  //可优化为List
        System.out.println("已将"+word+"的node查询结构存入redis,nodeId="+cntNode);
    }

    public void putOtherNode2Redis(String word,String s, Jedis client)
            throws IOException {
        HashMap<String, String> map = new HashMap<>();
        map.put(cntNode.toString(),s);
        client.hmset(word + "_node_neo4j", map);  //可优化为List
        System.out.println("已将"+word+"的node查询结构存入redis,nodeId"+cntNode);
        cntNode++;
    }

    public void putLink2Redis(String word, String category, String from, String to, double weight, Jedis client)
            throws IOException {
        LinkProto.Link.Builder linkBuilder =  LinkProto.Link.newBuilder();
        linkBuilder.setName(category);
        linkBuilder.setSource(from);
        linkBuilder.setTarget(to);
        linkBuilder.setWeight(weight);
        LinkProto.Link linkB = linkBuilder.build();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        linkB.writeTo(output);
        byte[] byteArray = output.toByteArray();
        String s =new String(byteArray,"ISO-8859-1");
        HashMap<String, String> map = new HashMap<>();
        map.put(cntLink.toString(),s);
        client.hmset(word + "_link_neo4j", map);  //可优化为List
        System.out.println("已将"+word+"的link查询结构存入redis,linkId"+cntLink);
        cntLink++;

    }

    public static void main(String[] args) throws IOException {
        RedisPooledConnection rpc = new RedisPooledConnection();
        Jedis client =rpc.getClient();
        Jedis client2 =rpc.getClient();
       // client.set("xxp","123");
        client.expire("xxp",5);
        System.out.println(client.get("xxp"));
        System.out.println(client2.get("xxp"));
       // System.out.println(client.get("123"));
    //    rpc.returnResource(client);
// 按照定义的数据结构，创建一个Person
        for(Integer i=0; i<2;i++){
            NodeProto.Node.Builder personBuilder =  NodeProto.Node.newBuilder();
            personBuilder.setName("叉叉哥"+i);
            personBuilder.setCategory(1);
            personBuilder.setLabel("Friend A");
            personBuilder.setValue(1.2);
            NodeProto.Node xxg = personBuilder.build();
            HashMap<String, String> map = new HashMap<>();
            // 将数据写到输出流，如网络输出流，这里就用ByteArrayOutputStream来代替
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            try {
                xxg.writeTo(output);
            } catch (IOException e) {
                e.printStackTrace();
            }

            // -------------- 分割线：上面是发送方，将数据序列化后发送 ---------------

            byte[] byteArray = output.toByteArray();
            String s =new String(byteArray,"ISO-8859-1");
            map.put(i.toString(),s);
            client.hmset("word",map);
        }

        // -------------- 分割线：下面是接收方，将数据接收后反序列化 ---------------
        Iterator<String> iter=client.hkeys("word").iterator();
               while (iter.hasNext()){
                       String key = iter.next();
                   ByteArrayInputStream input = new ByteArrayInputStream(client.hmget("word",key).get(0).getBytes("ISO-8859-1"));
                   NodeProto.Node xxg2 = NodeProto.Node.parseFrom(input);
                   System.out.println("name:" + xxg2.getName());
                   System.out.println("name:" + xxg2.getLabel());
                   System.out.println("name:" + xxg2.getCategory());
                   System.out.println("name:" + xxg2.getValue());
                    }
        rpc.returnResource(client);
//        // 接收到流并读取，如网络输入流，这里用ByteArrayInputStream来代替
//        ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
//
//        // 反序列化
//        NodeProto.NodeProto2 xxg2 = NodeProto.NodeProto2.parseFrom(input);
//        System.out.println("name:" + xxg2.getName());
//        System.out.println("email:" + xxg2.getLabel());


    //  Neo4jToRedis ntr = new Neo4jToRedis();
      //  ntr.test();

    }

}
